#include "aligndelegate.h"

AlignDelegate::AlignDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{}

void AlignDelegate::paint(QPainter *painter,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index); // 初始化样式选项
                                  // index.data().
    // 根据索引或其他条件动态设置对齐方式
    if (index.row() % 3 == 0) {
        opt.displayAlignment = Qt::AlignLeft;
    } else {
        opt.displayAlignment = Qt::AlignRight;
    }

    // 绘制 Item
    painter->save();
    QTextOption textOption;
    textOption.setAlignment(opt.displayAlignment);
    painter->setFont(opt.font);
    painter->drawText(opt.rect, opt.text, textOption);
    painter->restore();
}
